package com.krain.ddbb.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.krain.corelibrary.net.NetClient;
import com.krain.corelibrary.util.DeviceInfo;
import com.krain.corelibrary.util.JsonUtil;
import com.krain.corelibrary.util.SpUtil;
import com.krain.ddbb.constant.Constant;
import com.krain.ddbb.constant.SpKeys;
import com.krain.ddbb.entity.UserInfoBean;
import com.krain.ddbb.service.LocationService_;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;
import com.pingplusplus.android.PingppLog;

import org.androidannotations.annotations.EApplication;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * Class description
 *
 * @author yamin
 * @date 2015/11/3 上午 11:34
 */
@EApplication
public class APP extends Application {

    private UserInfoBean mUserInfo;

    @Override
    public void onCreate() {
        super.onCreate();
        PingppLog.DEBUG = true;
        initJpush();
        mUserInfo = new UserInfoBean();
        Logger.init(Constant.PROJECT_TAG).hideThreadInfo().logLevel(LogLevel.FULL);
        DeviceInfo.initialize(this);
        NetClient.initHttp(this, Constant.HOSTURL);
    }



    private void initJpush(){
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }

    public void setJpushTags(UserInfoBean userbean){
        JPushInterface.setAlias(getApplicationContext(), String.valueOf(userbean.getUid()), null);
    }


    /**
     * 判断用户是否登录
     * @return
     */
    public boolean isLogin() {
        UserInfoBean userInfoBean = getmUserinfo();
        if (userInfoBean.getAccess_token() == null) {
            return false;
        } else return true;
    }


    /**
     * @return 用户对象
     */
    public UserInfoBean getmUserinfo() {
        if (null == mUserInfo || TextUtils.isEmpty(mUserInfo.getAccess_token())) {
            String json = SpUtil.getString(SpKeys.USERINFO, "");
            if (!TextUtils.isEmpty(json))
                mUserInfo = JsonUtil.parseObject(json, UserInfoBean.class);
            if (TextUtils.isEmpty(json)) return mUserInfo;
            mUserInfo = JsonUtil.parseObject(json, UserInfoBean.class);
            return mUserInfo;
        } else return mUserInfo;
    }

    /**
     * 清除用户信息
     */
    public void clearUserInfo(){
        setmUserinfo(new UserInfoBean());
    }

    public void setmUserinfo(UserInfoBean mUserinfo) {
        this.mUserInfo = mUserinfo;
        if (mUserinfo != null) {
            String json = JsonUtil.toJSONString(mUserinfo);
            if (!TextUtils.isEmpty(json))
                SpUtil.putString(SpKeys.USERINFO, JsonUtil.toJSONString(mUserinfo));
        }
    }
}
